/**
 
 */
package member
import (
    "dhfshop/models"
	"github.com/astaxie/beego/orm"
)

//会员关联表
type MemberRelation struct {
	MemberId           int64     `orm:"column(member_id);pk" json:"member_id" form:"member_id"` 
    CardId             string  `orm:"column(card_id)" json:"card_id" form:"card_id"`               /* 会员卡号 */
	RefereesId         int64   `orm:"column(invi_member_id)" json:"invi_member_id" form:"invi_member_id"` /*推荐人（会员）*/
	RegisterPartnerId  int64   `orm:"column(reg_partner_id)" json:"reg_partner_id" form:"reg_partner_id"` /*注册关联商家编号*/
}

func init() {
    orm.RegisterModel(new(MemberRelation))   
}
// 获取会员关联
func GetRelation(memberId int) (ad *MemberRelation) {
	ad = &MemberRelation{}
	o := models.GetDefaultDB()
	err := o.QueryTable(new(MemberRelation)).Filter("member_id", memberId).One(ad)
	if err != nil {
	    return nil
	}
	return
}

// 保存绑定
func SaveRelation(v *MemberRelation) (err error) {
	o := models.GetDefaultDB()
	m := MemberRelation{MemberId: v.MemberId}
	if err = o.Read(&m); err == nil {
		if _, err = o.Update(v); err == nil {
		}
	} else {
	    _, err = o.Insert(v)
	}
	return
}


